//
//  BuildingTypeSelector.swift
//  TRHouse
//
//  Created by zhu xietong on 2018/5/6.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import UIKit
import Eelay
import JoLoading


class BuildingTypeSelector:FilterDropSelector,TableVConfig
{
    static var typeDatas:NSMutableArray? = nil
    
    override var intrinsicContentSize: CGSize{
        return [Swidth,400]
    }
    
    
    var LoadingClass: JoLoading.Type = SimpleLoading.self
    func touch(cell: Any, actionID: String, model: NSMutableDictionary) {
        
    }
    
    let leftTb = UITableView(frame: .zero, style: .plain)
    let rightTb = UITableView(frame: .zero, style: .plain)
    
    
    var lsection:TP.section{
        get{
            let sec:TP.section = [
                [
                ]
            ]
            return sec
        }
    }
    var rsection:TP.section{
        get{
            let sec:TP.section = [
                [
                ]
            ]
            return sec
        }
    }
    
    
    let lselector:TP.selector = [
        "<default>":LeftCell.self,
        ]
    
    let rselector:TP.selector = [
        "<default>":RightCell.self,
        ]
    
    var datas = NSMutableArray(){
        didSet{
            self.leftItem = nil
            self.rightItem = nil
            loadTypeData(list: datas)
        }
    }
    
    override func addLayoutRules() {
        

    
        let line = UIView()
        let line2 = UIView()
        
        let contenV = UIView()
        contenV.eelay = [
            [leftTb,[ee.T.L.B],190.co],
            [line,[leftTb,ee.R,ee.L],0.5,[ee.T.B]],
            [rightTb,[line,ee.R,ee.L],[ee.T.R.B]],
            [line2,[ee.T.L.R],"0.4"]
        ]
        contentView.backgroundColor = .white
        
        contentView.eelay = [
            [contenV,[ee.T.L.B.R]]
        ]

        leftTb.showsVerticalScrollIndicator = false
        line.backgroundColor = UIColor(shex:"#ccc")
        line2.backgroundColor = UIColor(shex:"#ccc")
        
        
        self.configTables(tables: leftTb,rightTb)
        leftTb.buildDynamicHeight()
        rightTb.buildDynamicHeight()
        
        leftTb.backgroundColor = UIColor.table_bg_color
        leftTb.separatorStyle = .none
        rightTb.separatorStyle = .none
        leftTb.load(sections: lsection, selector: lselector)
        rightTb.load(sections: rsection, selector: rselector)
        
        leftTb.register(Header.self, forHeaderFooterViewReuseIdentifier: "leftHeader")
        rightTb.register(Header.self, forHeaderFooterViewReuseIdentifier: "rightHeader")

    }
    
    func loadTypeData(list:NSMutableArray) {
        leftTb.model["section0"] = list
        leftTb.reloadData()
    }
    
    
    
    override func didShow() {
        if let list = BuildingTypeSelector.typeDatas
        {
            if datas.count <= 0 {
                self.datas = list.mutable_array
            }
        }
        else{
            mo_request_send()
        }
    }
    
    
    
    var leftItem:NSMutableDictionary? = nil{
        didSet(oldValue){
            guard let new = leftItem else{return}

            let list = DataNode.path(node: "list", new, value: NSMutableArray())
            list.list { (dict:NSMutableDictionary, i) in
                dict[FK.selected] = FK.NO
            }
            rightTb.model <-- ("section0",list,NodeInsert.arrayRefresh)
            rightTb.reloadData()
            
        }
    }
    
    var rightItem:NSMutableDictionary? = nil
    
        
    func mo_request_send() {
        weak var wself = self
        
        loadingV.loading(message: "", title: "")
        JoTask().log.log_response().post.url("sssdasdas").json_handle { (status, msg, obj, resp) in
            wself?.loadingV.dismiss()
            if status.isSuccess{
                if let list = DataNode<NSMutableArray>.path(node: "building_type", obj)
                {
                    BuildingTypeSelector.typeDatas = list
                    wself?.datas = list.mutable_array
                    return
                }
            }
//            wself?.mo_request_alert(msg:msg, title: "", bt_title: "重拾")
            }.run()
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
         let cell = tableView.cellForRow(at: indexPath) as? BaseCell
        cell?.ckecked = false

    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as? BaseCell
        cell?.ckecked = true

        guard let data = tableView.cellData(at: indexPath) else {
            return
        }
        if tableView === leftTb{
            leftItem = data
            
        }
        else{
            rightItem = data
            let obj = ["buildingType":leftItem!,"price":rightItem!]
            confirm(values: obj)
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if tableView === leftTb{
            let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "leftHeader") as? Header
            header?.titleL.text = "楼盘类型"
            return header
        }
        if tableView === rightTb{
            let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "rightHeader") as? Header
            header?.titleL.text = "均价范围/㎡"
            return header
        }
        return nil
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    

    
}

extension BuildingTypeSelector{
    
    class Header: JoHeaderFooterView {
        let titleL = UILabel()
        override func addLayoutRules() {
            contentView.eelay = [
                [titleL,[ee.L.Y.R,[AppTheme.edge,0,-AppTheme.edge]]],
            ]
            titleL.ui.font12.color(hex: UIColor.main_hex)
        }
    }
    
    class BaseCell: JoTableCell {
        
        open func updateCheck() {
            if mo.model[FK.selected,""] == "YES"
            {
                contentView.backgroundColor = UIColor.white
            }
            else{
                contentView.backgroundColor = UIColor.table_bg_color
            }
        }
        open var ckecked:Bool{
            set{
                mo.model[FK.selected] = newValue ? FK.YES : FK.NO
                self.updateCheck()
            }
            get{
                return mo.model[FK.selected,""] == FK.YES
            }
        }
        
        
    }
    class LeftCell: BaseCell {
        let nameL = UILabel()
        override func addLayoutRules() {
            contentView.eelay = [
                [nameL,[ee.T.L.B.R,[14,AppTheme.edge,-14,-AppTheme.edge]],"20"],
            ]
            _ = nameL.ui.font12.cl_000
            self.selectionStyle = .none
            
        }
        
        override func mo_model_reload() {
            nameL.text = mo.model["type_name",""]
            self.ckecked = mo.model[FK.selected,FK.NO] == FK.YES
        }
    }
    
    class RightCell: BaseCell {
        let nameL = UILabel()
        
        override func updateCheck() {
            if ckecked{
                nameL.textColor = UIColor(shex: UIColor.main_hex)
            }
            else{
                nameL.textColor = UIColor(shex: "#222")
            }
        }
        override func addLayoutRules() {
            
            
            contentView.eelay = [
                [nameL,[ee.T.L.B.R,[20,AppTheme.edge,-20,-AppTheme.edge]],"17"],
            ]
            __line.backgroundColor = UIColor(shex:"#ccc9")
            _ = nameL.ui.font13.cl_222
            //            self.selectionStyle = .default
            
        }
        
        override func mo_model_reload() {
            nameL.text = mo.model["max",""] + "-" + mo.model["min",""]
            updateCheck()
        }
    }
}
